package com.mongo.service;

import com.mongo.func.SFunction;
import com.mongo.service.impl.LambdaQuery;
import com.mongodb.client.gridfs.model.GridFSFile;
import com.mongodb.client.result.DeleteResult;
import org.bson.types.ObjectId;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.data.mongodb.gridfs.GridFsResource;

import java.io.InputStream;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;

/**
 * 提供mongo的增删改查和桶操作
 * @author xiazhun
 * @create 2022/6/10
 */
public interface BaseMapper <T> extends MongoMapper<T> {
    default T queryOne(Criteria criteria){
        return queryOne(new Query(criteria));
    }

    List<T> queryByIds(Collection<String> ids);

    GridFSFile queryById(ObjectId id);

    T queryOne(SFunction<T, ?> function, Object value);

    default T queryOne(String key, Object value){
        return queryOne(new Query(Criteria.where(key).is(value)));
    }

    GridFSFile gridQueryOne(Query query);

    GridFSFile gridQueryOne(SFunction<GridFSFile, ?> function, Object value);

    default GridFSFile GridQueryOne(String key, Object value){
        return gridQueryOne(Criteria.where(key).is(value));
    }

    default GridFSFile gridQueryOne(Criteria criteria){
        return gridQueryOne(new Query(criteria));
    }


    List<GridFSFile> gridListFind(Query query);

    default List<GridFSFile> gridList(Criteria criteria){
        return gridListFind(new Query(criteria));
    }

    void gridDel(Query query);

    default void gridDel(Criteria criteria){
        gridDel(new Query(criteria));
    }


    default List<T> list(String key, Object value){
        return list(Criteria.where(key).is(value));
    }

    List<T> list();

    default List<T> list(Criteria criteria){
        return list(new Query(criteria));
    }

    List<T> list(Criteria criteria,String collectionName);

    DeleteResult remove(T t);


    default long removeById(String id){
        return remove(Criteria.where("_id").is(id));
    }

    default long remove(Criteria criteria){
        return remove(new Query(criteria));
    }

    long save(Collection<T> collection);

    T save(T t, String collections);

    default PageImpl<T> page(Criteria criteria, PageRequest pageRequest){
        return page(new Query(criteria),pageRequest);
    }

    ObjectId gSave(InputStream inputStream, String filename);

    GridFsResource download(Query query);

    default GridFsResource download(Criteria criteria){
        return download(new Query(criteria));
    }

    InputStream getStream(Query query);

    default InputStream getStream(Criteria criteria){
        return getStream(new Query(criteria));
    }

    long size();

    default long size(Criteria criteria){
        return size(new Query(criteria));
    }
}
